package com.inner;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


/**
 * 发送Http Post请求
 * @author wuwenzhe
 *
 */
public class CardRealName {
//	private static final String POST_URL="http://172.30.252.10:9001/api/service";//测试环境
//	private static final String POST_URL="http://172.30.252.10:9108/api/smau";//自己上传的项目测试环境
//	private static final String POST_URL="http://172.16.3.100:9200/api/smau"; //正式环境
	private static final String POST_URL="http://localhost:9108/api/smau";//本地环境
//	private static final String POST_URL="http://localhost:9101/api/smau";//本地环境陈平
	//渠道的私钥,这里是编号为10008的渠道的私钥
//	private static final String CHANNEL_PRIVATE_KEY="30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810091c997463138fcb1f3611fc38d39878e665865ffc1fbbfe0bb03eb315a1896f3d98f874d72bb98b49631addcc2e884d9ff09e0299ccfc4ebc7cef52f2059d86fc9bd9a4a92a45969342d84821efeffd58e80045fe3db98e3a8f65aa238fa170890714fdcf00d2d60841c1261c56753c2958f4f6603d4d3e23db5cc42d6a2cbcf020301000102818022b80f2224c3cd0e360759d0b0d7d40c1a1c9041fdf90de39fb619b359a10486133866c10f8354a52125b8a19bfb52f435942d01cbfa010c23bc0a510c53c942ce1142b6aaaff71677b7e6d22ca92b39773addc978393d5ccf28666c6c409c989333db698fd3733f77e534ef56190e4410180a0589d9c20f54cb8abc8ba76cc5024100c4b1d8941555169774b9ba2fd4abc504c8c8ecfbb3afe4737c8896644229b5e32a4f3c8eb2d355b8bebcefb0d5c8280d4c906d49dd5a816cc1586f8bfc04cb8b024100bdbe66043f1e2c22cda8adda9826136e644365a00a4d6b229b3d60d486dbe9af3f6ecdc6fd505c94dd6a24f004dd568431f4f3204b43730ea319d9782213194d02410091315ad70cdc2f7e038ae9d64eddb0e474366540f385fe54057469d77e437de13f25186599b988c883fbc1cf3e2faee90a407e7c0ceab729099bf75354e8513b024040efd180308dc5f31f49543e9485ab6ec357a187151f8bc3eb37255576d5fa72e495aa4d28d280ecdd9dff0839d1fa5301b9e01f3442e034a933268f0bb2579d02403dac1f74b150e868265b8a53f47c8db1580459a60e771369bbcb83e507fde69ed189a4f238493911ec47eec8607378ca762edc7e13a7ebda54c75764ad4003f0";
//	private static final String CHANNEL_ID = "10009";
	//智慧验证平台的公钥
	private static final String SMAU_PUB_KEY="30819f300d06092a864886f70d010101050003818d0030818902818100a136bd1399f94e46ff42eb6fad480621ca3e03c01e9fc8e1eab6d75dabc63e47ef1c2064fb10abaa491b9452e2d504de88223775e14931c6d8c7aab0f13d6673f567d7d0b0769539a749d920f50d7421f3fd66b5425bc3dee6f27b82de6243894e79b76a478efe79ff44ee813e789150871498186cbf1672a306469d250b703d0203010001";
	
	
	//渠道的私钥,这里是编号为10098的渠道的私钥
	private static final String CHANNEL_PRIVATE_KEY="30820278020100300d06092a864886f70d0101010500048202623082025e02010002818100943b9c6232e038dd937047a8e53d6112c4bc714d42d6904bb29e654f74bd73b04af881caa1d5b363a4bf5105717e400d47ffe728e8daba6a9b0df0a14117d708cbb1cf89e060b9ec653990b6eb8a34228f451031cf60a479f971c3fe2072a1a2b75be09156a71e58998248d82c144d48c26af8b8827f271a7bc730e0ed2775a3020301000102818100837c8112661898b88b311c8f8790b38be771547b791f446db12a69a95a5f36c0992fe8496ad659c2b87f700a090b391bced3032439b0d38cfeaf15d4a41dd68239bd96e1f3274ed335c76122f021a9475bd6ada31591d51fdd494f397df06ff0a272518262dcc66099234d510f6948bd46b6547ee691a3b90667e7a74ad80389024100ec6bb0df4ebc84c39fac448ca11bb0842dcde0a7a3db4b3c3166991170befd3eeeb2125ad6f52c52de9293861850071c11c15f1f7fca550edb7ba09c458feead024100a0824461adc5671ded58753cb02e4b121bfbe151646ac06ab5e40d431bae3e665f1b6083848e5a8add678ea7925df41f596c31a6e0a5517580ed8ebf7f790f8f024028d6111f01429ecc0b707e06ac6d53f800d166d48e00fae5d2c29dfddd33a01a96b03b6b6565c3da2c87b4e39c9f761aeb163390054ab5967362cf432ee0c5a102410089a74e97eadcaa13bb72aa90d293d12405c5ddb5dd05c4617ffec5f4f0af5daa9414399071455897514c37c6b4bb48dafde8d12849eeddce8534b6c0bac50b37024100aedbb826069f0ae5ee71851446b2d327b9616a4f2f837198d6a420c29aa474707f9e5870c96cbb9135c798d5b377f799c3aa73a920b608af7fdfbe4802e11f65";
	private static final String CHANNEL_ID = "10098";
	//渠道的私钥,这里是编号为0000000003的渠道的私钥
//		private static final String CHANNEL_PRIVATE_KEY="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJFxnVysj9bsS5YQyFPk0If2s/seMAEm6n2mqK07raeOO6bY2WWuMtRrMHF32xxM0l1dnD5W3bO/jUKH7dMKXfYSQhnR/YPLA6lCKlCXXTuLSHy8gOZqwR0ZKxVN1UtiWwcp1+lV99C+LNm/dpyKo/6/8jN2MxYU/NaQS+RW/pEpAgMBAAECgYBRe4FZpiN/mSpPjF9vAMYDJid3DFaAjrjYZEIjq0eCk6C/vMUuvgUkFFkzeJ58oF1KiWM7WZcjWLFfEKAgl7JDjXPjqn+NgG80OxaPXrbFaTK2kpDVNOjgnOK4UTlZRujmdz11N+sggcM2OLhkWBClRez2AfPv7sfMCxsuPe57cQJBAMZy+/ILRjps3VuD8mvSXKVmy9kQs6JSJfOo7Yr03z0/lJ6SU+ZCL+Kvw+ken/Rjjrvl7yQiEWmsAW3lcJDyC/cCQQC7n3kkK4ap9ruKpmgP44gFiR1e4egN3Ulhj0hyyycr+xXy92fTuD88r7SO7X8GKpcu19WpYC8gV9AtelQG/8PfAkEAiqOgAjwAJycRCjNHNC51jRQgy5fQd1E+tFhLA64JG8gEcze1J4ML4yl6C63af6jh+cEsbajjU1jYcsc8+fYvhwJBALSF51gu7P4B4Uaxf/SxtwGkgPb3I4CbhuxwoYOaBlGh3+eLtoGWiHnKkU7oIkz7k8VFdFXors9OzoXDaq/lTT8CQAjobhMUhNBM13ZxyWwnWb4485JeUhvIShV+pKIqG8Xo1dncYJEMTsTFDTRKbjdANTssQDmGsL1qd1Qm2Wu+AjY="
		
	
	
	
	private static final String CONTENT_TYPE_TEXT = "application/json";
	public static void main(String[] args) throws Exception{
		String postMsg=getRequestMsg();
		
		CloseableHttpClient httpclient = HttpClients.custom().build();
        try {
        	
            HttpPost httpPost = new HttpPost(POST_URL);
            
            System.out.println("Executing request " + httpPost.getRequestLine());
    		StringEntity se = new StringEntity(postMsg, "UTF-8");
    		se.setContentType(CONTENT_TYPE_TEXT);
    		se.setContentEncoding( "UTF-8");
    		httpPost.setEntity(se);
    	//	System.out.println("test"+httpPost.toString());
    		for(int i=0;i<1;i++){
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println(response.getStatusLine());
                if(200==(response.getStatusLine().getStatusCode())){
	                String respStr=IOUtils.toString(entity.getContent());
	                //进行报文签名的验证
	                System.out.println(respStr);
	                boolean result=PackAndUnpackDemo.verify(respStr, SMAU_PUB_KEY);
	                System.out.println("结果报文签名验证结果为:"+result);
                }
            } finally {
                response.close();
            }
    		}
        } finally {
            httpclient.close();
        }
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public static String getRequestMsg() throws Exception{
		Map<String,String> body=new HashMap<String,String>();
//		body.put("cardNo", "6226580008234052");
//		body.put("cardNo", "6226580008232726");
//		body.put("cardNo", "6223930803544473");
//		body.put("name", "吴亚楠");
		
		//贵州银联匹配
//		body.put("cardNo", "5220010100334632");
//		body.put("certType", "01");
//		body.put("certNo", "230405198210030033");
//		body.put("name", "杨华");	
//		body.put("phoneNo", "15811217851");
		
//		
//		body.put("cardNo", "6212261001066946770");
//		body.put("certType", "01");
//		body.put("certNo", "22072219920224482X");
//		body.put("name", "吴亚楠");	
//		body.put("phoneNo", "13817840673");
		
		
		body.put("cardNo", "5288560022551092");
		body.put("certType", "01");
		body.put("certNo", "650203198810090034");
		body.put("name", "艾力凯木·艾再孜");	
		body.put("phoneNo", "13309900998");
		
//		body.put("cardNo", "6228930001027481005");
//		body.put("certType", "01");
//		body.put("certNo", "522123197207182039");
//		body.put("name", "吴汉强");	
//		body.put("phoneNo", "13885220665");
		
		
		//ULINK匹配
//		body.put("cardNo", "4392250802291392");
//		body.put("certType", "01");
//		body.put("certNo", "330302197801311632");
		//不匹配
//		body.put("cardNo", "6215683400009368705"); 
//		body.put("name", "贵州");
//		body.put("name", "贵州");
//		body.put("name", "贵州");
//		body.put("userNo","1");
//		body.put("srcMerId", "1");
//		body.put("cardNo", "1");
//		body.put("phoneNo", "13712651767");
//		body.put("certType", "01");
//		body.put("certNo", "511522199802080047");
		//String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "RISK001", "银行卡消费区间验证");
//		String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "00005", "厦门渠道");
//		String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "00011", "银行卡验证");
		String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, CHANNEL_ID, "00011", "银行卡验证");
//		String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10098", "00005", "厦门渠道");
		
		
		System.out.println(msg);
		return msg;
	}
}
